package com.team.databoard.controller;

import com.team.databoard.common.BaseResponse;
import com.team.databoard.common.ResultUtils;
import com.team.databoard.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/result")
public class ResultController {

    @Resource
    private StudentService studentService;

    @GetMapping({"/lastname/{provinceId}", "/lastname"})
    public BaseResponse<List<Map<String, Object>>> getLastNameStats(@PathVariable(required = false) Integer provinceId) {
        return ResultUtils.success(studentService.groupByLastName(provinceId));
    }

    @GetMapping({"/sex/{provinceId}", "/sex"})
    public BaseResponse<Map<String, Integer>> getSexStats(@PathVariable(required = false) Integer provinceId) {
        return ResultUtils.success(studentService.groupBySex(provinceId));
    }

    @GetMapping({"/province/{provinceId}", "/province"})
    public BaseResponse<Map<String, Integer>> getProvinceStats(@PathVariable(required = false) Integer provinceId) {
        return ResultUtils.success(studentService.groupByProvince(provinceId));
    }

    @GetMapping({"/birthmonth/{provinceId}", "/birthmonth"})
    public BaseResponse<Map<String, Integer>> getBirthMonthStats(@PathVariable(required = false) Integer provinceId) {
        return ResultUtils.success(studentService.groupByBirthMonth(provinceId));
    }

    @GetMapping({"/birthyear/{provinceId}", "/birthyear"})
    public BaseResponse<Map<String, Integer>> getBirthYearStats(@PathVariable(required = false) Integer provinceId) {
        return ResultUtils.success(studentService.groupByBirthYear(provinceId));
    }

    @GetMapping({"/height/{provinceId}", "/height"})
    public BaseResponse<Map<String, Integer>> getHeightStats(@PathVariable(required = false) Integer provinceId) {
        return ResultUtils.success(studentService.groupByHeight(provinceId));
    }

    @GetMapping({"/education/{provinceId}", "/education"})
    public BaseResponse<Map<String, Integer>> getEducationStats(@PathVariable(required = false) Integer provinceId) {
        return ResultUtils.success(studentService.groupByEducation(provinceId));
    }

    @GetMapping("/total")
    public BaseResponse<Integer> getTotal() {
        return ResultUtils.success(studentService.getStudentCount());
    }

    @GetMapping({"/update_list/{count}", "/update_list"})
    public BaseResponse<Map<String, Object>> getUpdateList(@PathVariable(required = false) Integer count) {
        if (count == null) {
            // 默认值为10
            count = 10;
        }
        return ResultUtils.success(studentService.getUpdateList(count));
    }
}
